package mlProject.classifier.svm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import featureSelect.FeatureSelect;
import featureSelect.InfoGainWithSentiment;
import mlProject.DocModel;
import mlProject.DocModelReader;

public class MyLibSvm {
	public static void main(String[] args) throws IOException {
		int numFrature=650;
		transToSvmFormat(numFrature,"newTrain.txt","src/main/resources/svm/train.txt");
		transToSvmFormat(numFrature,"newTest.txt","src/main/resources/svm/test.txt");
		svmStart();
	}

	public static void transToSvmFormat(int numFrature,String filePathBeforeTrans,String filePathAfterTrans) {
		FeatureSelect fs = new FeatureSelect();
		ArrayList<DocModel> docs = DocModelReader.readFromFile(filePathBeforeTrans);
		DocModel doc = null;
		//ArrayList<String> feature = InfoGainWithSentiment.getFeatureSelected(numFrature);//////////>>>>>>>>>>>
		ArrayList<String> feature = fs.getFreature("newTrain.txt",numFrature);
		//ArrayList<String> feature = InfoGainWithSentiment.getFeatureSelectedWholeIG(numFrature);//////////>>>>>>>>>>>
		Map<String, Integer> feature_index = new HashMap<String, Integer>();
		ArrayList<Double> value = new ArrayList<Double>();
		File file_out = new File(filePathAfterTrans);
		BufferedWriter bw_out;
		try {
			Integer count = 0;
			for(int i=0;i<feature.size();i++){
				feature_index.put(feature.get(i), count);
				value.add(0.0);
				count++;
				if (count == numFrature)
					break;
			}
			bw_out = new BufferedWriter(new FileWriter(file_out, false));
			for (int i = 0; i < docs.size(); i++) {
				doc = docs.get(i);
				for (int j = 0; j < value.size(); j++) {
					value.set(j, 0.0);
				}
				for (int j = 0; j < doc.features.size(); j++) {
					String featureName = doc.features.get(j).name;
					Double featureValue = doc.features.get(j).value;
					int index = 0;
					if (feature_index.containsKey(featureName)) {
						index = feature_index.get(featureName);
						value.set(index, value.get(index) + featureValue);
					}
				}
				if(doc.label.equals("positive"))
					bw_out.write("1");
				else
					bw_out.write("0");
				for (Integer j = 0; j < value.size(); j++) {
					bw_out.write(" " + (j + 1) + ":" + value.get(j));
				}
				bw_out.newLine();
			}
			bw_out.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void svmStart() throws IOException {
		String[] arg = { "src/main/resources/svm/train.txt", // 存放SVM训练模型用的数据的路径
				"src/main/resources/svm/model.txt" }; // 存放SVM通过训练数据训练出来的模型的路径

		String[] parg = { "src/main/resources/svm/test.txt", // 这个是存放测试数据
				"src/main/resources/svm/model.txt", // 调用的是训练以后的模型
				"src/main/resources/svm/out.txt" }; // 生成的结果的文件的路径
		System.out.println("........SVM运行开始..........");
		// 创建一个训练对象
		svm_train t = new svm_train();
		// 创建一个预测或者分类的对象
		svm_predict p = new svm_predict();
		t.main(arg); // 调用
		p.main(parg); // 调用
	}
}
